package com.chj.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chj.constants.Constants;
import com.chj.dao.UserDAO;
import com.chj.entity.User;
import com.chj.service.UserService;
import com.chj.util.GenerateSequenceUtil;

@Service
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserDAO userDAO;

	@Override
	public boolean login(String userId, String userPassword) {
		
		User user = userDAO.get(userId, userPassword);

		if (user != null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean checkUserExist(User user) {

		String userId = user.getUserId();

		if (userDAO.checkUserIdExist(userId)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean addUser(User user) {
	
		String userPk = GenerateSequenceUtil.getUUID();
		user.setUserPk(userPk);
		if (userDAO.addUser(user)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public User getUser(String userId, String password) {
		return userDAO.get(userId, password);
	}

    @Override
    public User getAdmin(String userId, String password) {
        
        return userDAO.getAdmin(userId, password);
    }

    @Override
    public List<User> getUserListBy(String userName
                                  , String userPhone
                                  , String userEmail
                                  , String dateBegin
                                  , String dateEnd
                                  , int    userType
                                  , int    page
                                  , int    rows) {

        return userDAO.getUserListBy(userName
                                   , userPhone
                                   , userEmail
                                   , dateBegin
                                   , dateEnd
                                   , userType
                                   , page
                                   , rows);
    }

    @Override
    public int getUserListSizeBy(String userName
                               , String userPhone
                               , String userEmail
                               ,String dateBegin
                               ,String dateEnd
                               , int    userType) {

        return userDAO.getUserListSizeBy(userName
                                       , userPhone
                                       , userEmail
                                       , dateBegin
                                       , dateEnd
                                       , userType);
    }

    @Override
    public int changeAdminPassword(User admin) {
        return userDAO.update(admin);
    }

    @Override
    public int resetUserPassword(String userPk) {

        User user = userDAO.getUserByUserPk(userPk);

        user.setUserPassword(Constants.RESET_USER_PASSWORD);

        return userDAO.update(user);
    }

    @Override
    public int changeUserPassword(User user) {
        
        user.setModifyDate(new Date());

        return userDAO.update(user);
    }

    @Override
    public int changeUserData(User user) {

        return userDAO.update(user);
    }


}
